package com.aaa.commonmethods;

/**
 * @author C-Q
 * @version 1.1.0
 * @Description:
 *        public static void yield()
 *       当前线程主动放弃时间片，回到就绪状态，竞争下一次时间片
 * @date 2022/11/1
 */
public class YieldDemo extends Thread{

    @Override
    public void run() {
        super.run();
        for (int i = 0; i < 10; i++) {
            Thread.yield();
            //放弃当前时间片，进入就绪状态，抢夺下一次资源；
            //使用该方法，交替执行的概率增加
            System.out.println(Thread.currentThread().getName()+"--------"+i);
        }
    }
}
class Test01{
    public static void main(String[] args) {
        YieldDemo yieldDemo1 = new YieldDemo();
        YieldDemo yieldDemo2 = new YieldDemo();
        yieldDemo1.start();
        yieldDemo2.start();
    }
}